package Document;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import com.mysql.jdbc.Statement;

/**
 * Servlet implementation class Servlettconnexion
 */
public class ServletConnexion extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletConnexion() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		DAO dao = new DAO();
		dao.setDriver("com.mysql.jdbc.Driver");
		dao.setChaineConnexion("jdbc:mysql:///document");
		dao.setLogin("root");
		dao.setPassword("");
        HttpSession session = request.getSession(true);
        String Nom = request.getParameter("Nom");
        String Prenom = request.getParameter("Prenom");
        String Mail = request.getParameter("Email");
        String Password = request.getParameter("Mdp");
        
        session.setAttribute("Nom", Nom); 
        session.setAttribute("Prenom", Prenom); 
        session.setAttribute("Email", Mail);
        session.setAttribute("Mdp", Password);
        boolean testco = false;
        try
        {
        	testco = verifUtilisateur(Mail, Password, dao);
		} 
        catch (ClassNotFoundException | SQLException e) {

			e.printStackTrace();
		}
  
        if(testco == true)
        {
        	getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        }
        else
        {
           	session.setAttribute("Email", null);
           //	System.out.println("Email"+Mail);
        	getServletContext().getRequestDispatcher("/connexion.jsp").forward(request, response);
        }
               
	}


public boolean verifUtilisateur(String Email, String Mdp, DAO connector) throws ClassNotFoundException, SQLException {
		
    String url = "jdbc:mysql:///document";
    String utilisateur = "root";
    String motDePasse = "";
    String requete="SELECT Mail, Mdp FROM inscription WHERE Mail = '?' AND Mdp = '!';";
    boolean co = false;
    
		if(Email != null)
		{
		

	    requete = requete.replace("?", Email);
	    requete = requete.replace("!", Mdp);
	    Connection connexion=connector.getConnexion();
	    Statement statement = null;
	    ResultSet resultat = null;
	    connexion = DriverManager.getConnection( url, utilisateur, motDePasse );
	    statement = (Statement) connexion.createStatement();
	    resultat = statement.executeQuery(requete);

	
	    if ( resultat.next() ) 
	    {
	       co = true;
	    }
	    else
	    {
	    	co= false;
	 
	    }
			
		
		return co;
	
			
	}
	else
	{
		return co;		
	}
 }
}
